From d7839765dc724623b6c5d23a1416599ab1c2c7c7 Mon Sep 17 00:00:00 2001 From: "kfraser@localhost.localdomain" Date: Wed, 11 Jul 2007 10:49:43 +0100 Subject: [PATCH] [XM] Tools support for extensions of the Xen-API for managing security policies This patch adds a couple of new commands for using the Xen-API extensions for security policies. Older tools are converted to support going through the Xen-API for their operations rather than doing the operations directly in their own code. Signed-off-by: Stefan Berger --- tools/python/xen/xm/activatepolicy.py | 86 ++++++++++++++++++ tools/python/xen/xm/addlabel.py | 123 ++++++++++++++++++++------ tools/python/xen/xm/cfgbootpolicy.py | 72 +++++++++++---- tools/python/xen/xm/create.dtd | 7 +- tools/python/xen/xm/create.py | 22 ++--- tools/python/xen/xm/getlabel.py | 47 +++++++--- tools/python/xen/xm/getpolicy.py | 94 ++++++++++++++++++++ tools/python/xen/xm/labels.py | 37 +++++++- tools/python/xen/xm/loadpolicy.py | 32 ++++++- tools/python/xen/xm/main.py | 70 ++++++++++----- tools/python/xen/xm/makepolicy.py | 14 ++- tools/python/xen/xm/resources.py | 33 +++++-- tools/python/xen/xm/rmlabel.py | 65 ++++++++++---- tools/python/xen/xm/setpolicy.py | 117 ++++++++++++++++++++++++ tools/python/xen/xm/xenapi_create.py | 55 ++++++++++++ 15 files changed, 746 insertions(+), 128 deletions(-) create mode 100644 tools/python/xen/xm/activatepolicy.py create mode 100644 tools/python/xen/xm/getpolicy.py create mode 100644 tools/python/xen/xm/setpolicy.py diff --git a/tools/python/xen/xm/activatepolicy.py b/tools/python/xen/xm/activatepolicy.py new file mode 100644 index 0000000000..46b1b920eb --- /dev/null +++ b/tools/python/xen/xm/activatepolicy.py @@ -0,0 +1,86 @@ +#============================================================================ +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (C) 2007 International Business Machines Corp. +# Author: Stefan Berger +#============================================================================ + +"""Activate the managed policy of the system. +""" + +import sys +from xen.util import xsconstants +from xml.dom import minidom +from xen.xm.opts import OptionError +from xen.xm import getpolicy +from xen.xm import main as xm_main +from xen.xm.main import server + +def help(): + return """ + Usage: xm activatepolicy [options] + + Activate the xend-managed policy. + + The following options are defined: + --load Load the policy into the hypervisor. + --boot Have the system boot with the policy. Changes the default + title in grub.conf. + --noboot Remove the policy from the default entry in grub.conf. + """ + +def activate_policy(flags): + policystate = server.xenapi.XSPolicy.get_xspolicy() + xs_ref = policystate['xs_ref'] + if int(policystate['type']) == 0 or xs_ref == "": + print "No policy is installed." + return + rc = int(server.xenapi.XSPolicy.activate_xspolicy(xs_ref, flags)) + if rc == flags: + print "Successfully activated the policy." + else: + print "An error occurred trying to activate the policy: %s" % \ + xsconstants.xserr2string(rc) + +def remove_bootpolicy(): + server.xenapi.XSPolicy.rm_xsbootpolicy() + +def main(argv): + if xm_main.serverType != xm_main.SERVER_XEN_API: + raise OptionError('xm needs to be configured to use the xen-api.') + flags = 0 + c = 1 + + while c < len(argv): + if '--boot' == argv[c]: + flags |= xsconstants.XS_INST_BOOT + elif '--load' == argv[c]: + flags |= xsconstants.XS_INST_LOAD + elif '--noboot' == argv[c]: + remove_bootpolicy() + else: + raise OptionError("Unknown command line option '%s'" % argv[c]) + c += 1 + + if flags != 0: + activate_policy(flags) + + getpolicy.getpolicy(False) + +if __name__ == '__main__': + try: + main(sys.argv) + except Exception, e: + sys.stderr.write('Error: %s\n' % str(e)) + sys.exit(-1) diff --git a/tools/python/xen/xm/addlabel.py b/tools/python/xen/xm/addlabel.py index 2d42d6702d..9e93641284 100644 --- a/tools/python/xen/xm/addlabel.py +++ b/tools/python/xen/xm/addlabel.py @@ -25,17 +25,29 @@ import sys from xen.util import dictio from xen.util import security from xen.xm.opts import OptionError +from xen.util import xsconstants +from xen.xm import main as xm_main +from xen.xm.main import server def help(): return """ Format: xm addlabel